package com.ymatou.iapi.csim.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.GetHistorySessionListResp;
import com.ymatou.iapi.csim.parameter.NavigateHistorySessionBean;
import com.ymatou.iapi.csim.parameter.SendMessageCSBean;
import com.ymatou.iapi.csim.parameter.GetHistorySessionListResp.cssession;
import com.ymatou.iapi.csim.service.NavigateHistorySessionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
/**
*查询符合条件的上一个或下一个会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_NavigateHistorySession{
	private static NavigateHistorySessionBean bean;
	private static NavigateHistorySessionCall call;
	private CsimCallService service=new CsimCallService();
	private Integer csId=Integer.parseInt(EnvSetup.getData("csId").toString());
	private static Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private  ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询符合条件的上一个或下一个会话");
	}

	@Before
	public void caseUp() {
		bean=new NavigateHistorySessionBean();
		call=new NavigateHistorySessionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NavigateHistorySession_001() {
		Logger.start(true,"查询符合条件的上一个或下一个会话,验证下一条");
		try {
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			bean.setStatus(cssession.getStatus());
			bean.setCsId(cssession.getCSId());
			bean.setUserId(cssession.getUserId());
			bean.setId(cssession.getId());
			bean.setSessionNumber(cssession.getSessionNumber());
			bean.setFromUserType(cssession.getUserType());
			bean.setFromPlatform(cssession.getFromPlatform());
			bean.setOpenType(cssession.getOpenType());
			bean.setBeginLaunchTime( new DateTime(cssession.getLaunchTime()).plusDays(-2).toDate());
			bean.setEndLaunchTime(new DateTime(cssession.getLaunchTime()).plusDays(2).toDate());
			bean.setBeginTime( new DateTime(cssession.getBeginTime()).plusDays(-2).toDate());
			bean.setLastBeginTime(new DateTime(cssession.getLastMessageTime()).plusDays(-1).toDate());
			bean.setDirection(1);//0:上一条 1：下一条
			
			call.setData(bean);
			call.callService();			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_NavigateHistorySession_002() {
		Logger.start(true,"查询符合条件的上一个或下一个会话,验证上一条");
		try {
			//客服上线
			service.getOnlineCS(csId);
			//打开会话
			ymt_cssession cssession=service.getOpenSession(csId,userId);
			//发消息
			SendMessageCSBean sendMessageCSBean=service.setSendMessageCSBean();
			sendMessageCSBean.setSessionId(cssession.getId());
			sendMessageCSBean.setCsId(csId);
			sendMessageCSBean.setUserId(userId);
			service.setSendMessageCSCall(sendMessageCSBean);
			
			bean.setStatus(cssession.getStatus());
			bean.setCsId(cssession.getCSId());
			bean.setUserId(cssession.getUserId());
			bean.setId(cssession.getId());
			bean.setSessionNumber(cssession.getSessionNumber());
			bean.setFromUserType(cssession.getUserType());
			bean.setFromPlatform(cssession.getFromPlatform());
			bean.setOpenType(cssession.getOpenType());
			bean.setBeginLaunchTime( new DateTime(cssession.getLaunchTime()).plusDays(-2).toDate());
			bean.setEndLaunchTime(new DateTime(cssession.getLaunchTime()).plusDays(2).toDate());
			bean.setBeginTime( new DateTime(cssession.getBeginTime()).plusDays(-2).toDate());
			bean.setLastBeginTime(new DateTime(cssession.getLastMessageTime()).plusDays(1).toDate());
			bean.setDirection(0);//0:上一条 1：下一条
			
			call.setData(bean);
			call.callService();			
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void verifyReturn()
	{
	 List<ymt_cssession> cssessionList=getNavigateSession();
      cssession resp=call.GetNavigateHistorySession();

      Logger.verifyEquals("200", call.getStatus(), "验证返回的Status");
//      for(int i=0;i<call.GetNavigateHistorySession();i++)
//      {
    	  cssession csReturn=call.GetNavigateHistorySession();
    	  ymt_cssession cs=cssessionList.get(0);
    	  Logger.verifyEquals(cs.getId(), csReturn.getSessionId(), "验证返回的sessionId");
    	  Logger.verifyEquals(cs.getSessionNumber(), csReturn.getSessionNumber(), "验证返回的SessionNumber");
    	  Logger.verifyEquals(cs.getCSId(), csReturn.getCSId(), "验证返回的CSId");
    	  Logger.verifyEquals(cs.getUserId(), csReturn.getUserId(), "验证返回的UserId");
    	  Logger.verifyEquals(cs.getUserType(), csReturn.getUserType(), "验证返回的UserType");
    	  Logger.verifyEquals(dateFormat.format(cs.getLaunchTime()), csReturn.getLaunchTime(), "验证返回的LaunchTime");
    	  
    	  Logger.verifyEquals(dateFormat.format(cs.getBeginTime()), csReturn.getBeginTime(), "验证返回的BeginTime");
    	  Logger.verifyEquals(cs.getEndTime()==null?null:dateFormat.format(cs.getEndTime()), csReturn.getEndTime(), "验证返回的EndTime");
    	  Logger.verifyEquals(cs.getStatus(), csReturn.getStatus(), "验证返回的Status");
    	  Logger.verifyEquals(cs.getOpenType(), csReturn.getOpenType(), "验证返回的OpenType");
    	  Logger.verifyEquals(cs.getCloseType()==null?0:cs.getCloseType(), csReturn.getCloseType(), "验证返回的CloseType");
    	  Logger.verifyEquals(cs.getFromPlatform(), csReturn.getFromPlatform(), "验证返回的FromPlatform");
    	  Logger.verifyEquals(cs.getIsSendScore(), csReturn.getIsSendScore(), "验证返回的IsSendScore");
    	  Logger.verifyEquals(cs.getScore(), csReturn.getScore(), "验证返回的Score");
    	  Logger.verifyEquals(cs.getMark()==null?0:cs.getMark(), csReturn.getMark(), "验证返回的Mark");
    	  Logger.verifyEquals(cs.getFromCSId(), csReturn.getFromCSId(), "验证返回的FromCSId");
    	  Logger.verifyEquals(cs.getMessageSource(), csReturn.getMessageSource(), "验证返回的MessageSource");
    	  Logger.verifyEquals(cs.getSourceParams(), csReturn.getSourceParams(), "验证返回的SourceParams");
    	  Logger.verifyEquals(cs.getQuestionGuideId(), csReturn.getQuestionGuideId(), "验证返回的QuestionGuideId");
     // }

	}
	private  List<ymt_cssession> getNavigateSession()
	{
		SimpleDateFormat dateFormat_MinValue = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date DateTime_MinValue = DateTime.parse("0001-01-01",  DateTimeFormat.forPattern("yyyy-MM-dd")).toDate();
		StringBuilder sqlwhere=new StringBuilder();
		StringBuilder subsqlwhere=new StringBuilder();
	   if (bean.getStatus() != -1) {
            sqlwhere.append(" AND Status=" + bean.getStatus());
        }
        if (bean.getCsId() != 0) {
            sqlwhere.append(" AND CSId=" + bean.getCsId());
        }
        if (bean.getUserId() != 0) {
            sqlwhere.append(" AND UserId=" + bean.getUserId());
        }
        if (bean.getFromUserType() != -1) {
            sqlwhere.append(" AND UserType=" + bean.getFromUserType());
        }
        
        if (bean.getId() != null && bean.getId()!="") {
            sqlwhere.append(" AND Id='" + bean.getId()+"'");
        }
        
        if (bean.getSessionNumber() != null && bean.getSessionNumber()!="") {
            sqlwhere.append(" AND SessionNumber='" + bean.getSessionNumber()+"'");
        }
        
        if (bean.getOpenType() != -1) {
            sqlwhere.append(" AND OpenType=" + bean.getOpenType());
        }
        if (bean.getFromPlatform() != -1) {
            sqlwhere.append(" AND FromPlatform=" + bean.getFromPlatform());
        }
        
        if (bean.getBeginLaunchTime() != null && bean.getBeginLaunchTime().after(DateTime_MinValue)) {
            sqlwhere.append(" AND LaunchTime>='" + dateFormat.format(bean.getBeginLaunchTime()) + "'");
        }
        if (bean.getEndLaunchTime() != null && bean.getEndLaunchTime().after(DateTime_MinValue)) {
            sqlwhere.append(" AND LaunchTime<='" + dateFormat.format(bean.getEndLaunchTime()) + "'");
        }
        if (bean.getBeginTime() != null && bean.getBeginTime().after(DateTime_MinValue)) {
            sqlwhere.append(" AND BeginTime>='" + dateFormat.format(bean.getBeginTime()) + "'");
        }
        if (bean.getEndTime() != null && bean.getEndTime().after(DateTime_MinValue)) {
            sqlwhere.append(" AND EndTime<='" + dateFormat.format(bean.getEndTime()) + "'");
        }
        if(bean.getDirection()==1)//下一条
        {
        	subsqlwhere.append("  BeginTime > '" + dateFormat.format(bean.getLastBeginTime()) + "' ");
        }
        else  if(bean.getDirection()==0)//上一条
        {
        	subsqlwhere.append("   BeginTime < '" + dateFormat.format(bean.getLastBeginTime()) + "' ");
        }
        List<ymt_cssession>  cssessionList=csSessionWapper.getNavigateSession(sqlwhere.toString(),subsqlwhere.toString());
        return cssessionList;
	}
	
}